package itsec1.api.cardmanagement;

import itsec1.customermanagement.CustomerManagement;

import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;




public class CardManagementListener implements Runnable {
	private CustomerManagement cm;
	private ServerSocket serverSocket;
	
	public CardManagementListener(CustomerManagement cm, ServerSocket serverSocket){
		this.cm= cm;
		this.serverSocket= serverSocket;
	}
	
	@Override
	public void run() {
		Socket socket= null;
		
		while(true){
			//accept connections/requests
			try{
				//stop listening, if customer manager is shutting down
				if(this.cm.getExecutorService().isShutdown())
					break;
									
				socket= serverSocket.accept();
				
				this.cm.getExecutorService().execute(new CardManagementRequestHandler(this.cm,socket));
			}catch(SocketException se){
				//shutting server down
			}catch(Exception e){
				System.out.println("Error: " + e.getMessage());
	            e.printStackTrace();
	            
	            if(socket.isClosed())
	            	break;
			}
		}
		
	}

}
